# def decodeString(s):
#     def decode(i):
#         # 给变了赋值
#         res = ""
#         num = 0
#         # while 循环
#         while i < len(s):
#             if s[i].isdigit():
#                 num = num * 10 + int(s[i])
#
#             elif s[i] == '[':
#                 i, tmp = decode(i + 1)
#                 res = res + num * tmp
#                 num = 0
#             elif s[i] == ']':
#                 return i, res
#
#             else:
#                 res = res + s[i]
#             # 条件控制语句
#             i += 1
#         # 返回值最终变量（或者是输出结果）
#         return i, res
#     return decode(0)[1]
#
# print(decodeString('3[ab]2[cd]'))




def decodeString(s):
    def decode(i):
        # 给变了赋值
        res = ""
        num = 0
        # while 循环
        while i < len(s):
            if "0" <= s[i] <= "9":
                num = num * 10 + int(s[i])

            elif s[i] == '[':
                i, tmp = decode(i + 1)
                res = res + num * tmp
                num = 0
            elif s[i] == ']':
                return i, res

            else:
                res = res + s[i]
            # 条件控制语句
            i += 1
        # 返回值最终变量（或者是输出结果）
        return i, res
    return decode(0)

print(decodeString('3[aa]2[cd]'))














